// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RRCONFIG_H
#define RRCONFIG_H

#include <string>
#include <map>
#include <atomic>

namespace rr {

    constexpr int WORKER_KEY_LENGTH_MAX = 100;

    class ClientLibConfig
    {
    public:

        class SOCKET_CLIENT
        {
        public:
            std::string     ipAddr;
            uint32_t        port;
			uint32_t        timeOut; 

            SOCKET_CLIENT()
            {
                ipAddr = "127.0.0.1";
                port   = 5555;
				timeOut = 10000;
            }

        } iSocketClient;

        SOCKET_CLIENT iSocketWorker;
        // 转码类型 0 CPU 1 GPU
        uint8_t encodeType = 0xFF;
        // 转码类型 0 编码 1 解码
        uint8_t resourceType = 0xFF;
        // 数据类型 0 H264 1 H265
        uint8_t dataType = 0xFF;
        // 规格类型 0 720P 1 1080P
        uint8_t flavorType = 0xFF;
        // 帧率类型 20 30 60
        uint8_t frameRate = 0xFF;
        // worker的接入状态 0 空闲，2 占用中
        std::atomic<uint32_t> status {0};
        std::string workerKey;
    };

    extern ClientLibConfig gClientConfig;

    class RrConfig {
    public:
        RrConfig() {
        }

        ~RrConfig() {
        }

        bool ReadConfig(const std::string &filename);

        std::string ReadString(const char *section, const char *item, const char *default_value);

        int ReadInt(const char *section, const char *item, const int &default_value);

        float ReadFloat(const char *section, const char *item, const float &default_value);

    private:
        bool IsSpace(char c);

        bool IsCommentChar(char c);

        void Trim(std::string &str);

        bool AnalyseLine(const std::string &line, std::string &section, std::string &key, std::string &value);

    private:
        //std::map<std::string, std::string> settings_;
        std::map <std::string, std::map<std::string, std::string>> settings_;
    };
}

#endif //RRCONFIG_H
